package com.buaa.hyf.util;

import android.database.Cursor;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Jeson on 2016/4/22.
 */
public class TxtDao {

public static void insertTxtDao(String fullPath){
    //先判断数据库里有没有这本书
    String sql = "select id from txt where full_path=?";

    Cursor c = Globals.util.getReadableDatabase().rawQuery(sql,new String[]{fullPath});
   //如果没有的话就将这本书插入到数据库中
    if(!c.moveToFirst()){
        sql = "insert into txt(full_path,now_page,over_flag) values(?,1,0)";

        Globals.util.getReadableDatabase().execSQL(sql,new Object[]{fullPath});//老师这里用的是Object类型的数组
    }
}

    //查询这个路径下的这本书的所有内容
    public static Map<String,Object> findTxtByFullpath(String fullPath){
        Map<String,Object> map = new HashMap<>();
        String sql = "select id,now_page,over_flag from txt where full_path = ?";
        Cursor c = Globals.util.getReadableDatabase().rawQuery(sql,new String[]{fullPath});
        c.moveToFirst();
        map.put("txtid",c.getInt(0));   //老师这里用的是c.getInt（）
        map.put("nowPage",c.getInt(1));
        map.put("overFlag",c.getInt(2));
        return map;
    }

    public static void updateTxtOverFlag(String fullPath){
        String sql = "update txt set over_flag=1 where full_path = ?";
        Globals.util.getReadableDatabase().execSQL(sql,new String[]{fullPath});
    }
}
